package com.example.demo003argsresolver.controller;

import com.example.demo003argsresolver.config.CurrUser;
import com.example.demo003argsresolver.domain.CurrUserVo;
import com.example.demo003argsresolver.domain.TranUserVo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Map;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public CurrUserVo hello(@CurrUser @RequestBody CurrUserVo currUserVo) {
        return currUserVo;
    }

    @GetMapping("/hello2")
    public Map hello2(@CurrUser Map map) {
        return map;
    }

    @GetMapping("/hello3")
    public Object hello3(@CurrUser Object obj) {
        return obj;
    }

    @GetMapping("/hello4")
    public TranUserVo hello4(TranUserVo tranUserVo) {
        return tranUserVo;
    }
    @PostMapping("/hello5")
    public TranUserVo hello5(TranUserVo tranUserVo) {
        return tranUserVo;
    }

    // Jackson 序列化字段映射策略
    public static void main(String[] args) throws Exception {
        A a = new A();
        a.setDemoAge(18);
        a.setDemoName("abc");
        a.setDemoNameAndAge("abc18");

        // 序列化对象
        String strJson = new ObjectMapper().writeValueAsString(a);
        System.out.println("strJson = " + strJson);//strJson = {"demo_name":"abc","demo_age":18,"diyProp":"abc18"}

        // 反序列化对象
        A newA = new ObjectMapper().readValue(strJson, A.class);
        System.out.println("newA = " + newA);//newA = HelloController.A(demoName=abc, demoAge=18, demoNameAndAge=abc18)
    }

    @Data
    @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
    static class A implements Serializable {
        private static final long serialVersionUID = 1L;
        private String demoName;
        private Integer demoAge;
        @JsonProperty("diyProp")
        private Object demoNameAndAge;
    }


}
